package com.easychat.controller.Admin;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.controller.ABaseController;
import com.easychat.entity.config.AppConfig;
import com.easychat.entity.constants.Constants;
import com.easychat.entity.dto.SysSettingDto;
import com.easychat.entity.enums.ResponseCodeEnum;
import com.easychat.entity.po.GroupInfo;
import com.easychat.entity.query.GroupInfoQuery;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.exception.BusinessException;
import com.easychat.redis.RedisComponent;
import com.easychat.service.GroupInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.io.IOException;

/**
 * @Author Dongdong
 * @注释 用户联系人
 */
@RestController("adminSysSettingController")
@RequestMapping("/admin")
public class AdminSysSettingController extends ABaseController {

    @Resource
    private RedisComponent redisComponent;
    @Resource
    private AppConfig appConfig;

    /**
     * 修改系统设置
     * @param sysSettingDto 修改后的系统设置
     * @param robotFile 机器人头像
     * @param robotCover 机器人封面
     * @return
     * @throws IOException
     */
    @RequestMapping("/saveSysSetting")
    @GlobalInterceptor
    public ResponseVO saveSysSetting(SysSettingDto sysSettingDto
                                    , MultipartFile robotFile
                                    , MultipartFile robotCover) throws IOException {
        //存储文件
        if (robotFile!=null){

            //文件夹路径
            String baseFolder = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE;
            //具体文件路径
            File targetFileFolder = new File(baseFolder+Constants.FILE_FOLDER_AVATAR_NAME);
            if (!targetFileFolder.exists()) {
                targetFileFolder.mkdirs();//创建文件夹
            }
            // 文件名
            String filePath =targetFileFolder.getPath()+"/"+sysSettingDto.getRobotUid()+Constants.IMAGE_SUFFIX;
            robotFile.transferTo(new File(filePath));
            robotCover.transferTo(new File(filePath+Constants.COVER_IMAGE_SUFFIX));
        }
        redisComponent.saveSysSettingDto(sysSettingDto);

        return getSuccessResponseVO("修改系统设置成功");

    }

    /**
     * 获取系统设置，  初次是默认值，后续改变存到redis从redis获取
     * @return
     */
    @RequestMapping("/getSysSetting")
    @GlobalInterceptor
    public ResponseVO getSysSetting() {
        SysSettingDto sysSettingDto = redisComponent.getSysSettingDto();
        //如果redis中为空，则new一个取默认值
        if (sysSettingDto==null){
           sysSettingDto=new SysSettingDto();
        }
        return getSuccessResponseVO(sysSettingDto);

    }
}
